﻿//----------------------------------------------------------------------------------------------------
// The GameManager is the base class for all managers
//----------------------------------------------------------------------------------------------------
package HG.Managers
{
	import HG.GameService;
	import HG.Debug;
	
	import HG.Visual.VisualNode;
	import HG.Visual.VisualConnection;
	import HG.Events.NodeEvent;
	
	public class VisibilityManager extends IGameManager
	{
		var m_levelManager:LevelManager;
		var m_playerManager:PlayerManager;
		
		//----------------------------------------------------------------------------------------------------
		public override function VisibilityManager()
		{
			
		}
		
		//----------------------------------------------------------------------------------------------------
		public override function Initialize()
		{
			m_levelManager = GameService.GetInstance().GetLevelManager();
			m_playerManager = GameService.GetInstance().GetPlayerManager();
			
			//m_levelManager.addEventListener(NodeEvent.CAPTURE, OnNodeCaptured);
		}
		
		//----------------------------------------------------------------------------------------------------
		public override function Shutdown()
		{
			
		}
		
		//----------------------------------------------------------------------------------------------------
		public override function OnTick(DeltaTime:Number)
		{
			
		}
		//----------------------------------------------------------------------------------------------------
		public function OnNodeCaptured(evt:NodeEvent)
		{
			// If the current player just caputred a node, show its tree.
			if (evt.m_node.GetIsOcupiedBy(m_playerManager.GetCurrentPlayerID()))
			{
				SetTreeVisible(evt.m_node.GetID());
			}
		}
		
		//----------------------------------------------------------------------------------------------------
		public function SetTreeVisible(nodeID:Number)
		{
			SetVisible(nodeID, false);
			
			var node:VisualNode = m_levelManager.GetVisualNode(nodeID);
			
			var cons:Array = node.GetConnections();
			
			var index:Number = 0;
			var listLength:Number = cons.length;
			
			for (index = 0; index < listLength; ++index)
			{
				var conID:Number = cons[index];
				SetVisible(conID, true);
				SetConnectionVisible(nodeID, conID);
			}
		}
		
		//----------------------------------------------------------------------------------------------------
		public function SetConnectionVisible(fromID:Number, toID:Number)
		{
			var visCon:VisualConnection = m_levelManager.GetVisualConnection(fromID, toID);
			
			Debug.ASSERT(visCon, "Bad conenction");
			
			var node:VisualNode = m_levelManager.GetVisualNode(toID);
			visCon.SetVisibleDelay(true, 750);
		}
		
		//----------------------------------------------------------------------------------------------------
		public function SetVisible(nodeID:Number, delay:Boolean)
		{
			var visNode:VisualNode = m_levelManager.GetVisualNode(nodeID);
			
			if (delay)
			{
				visNode.SetVisibleDelay(true, 900);
			}
			else
			{
				visNode.SetVisible(true);
			}
		}
		
		//----------------------------------------------------------------------------------------------------
	}
}